home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 24 / AACD 24.iso / AACD / System / ReqToolsLib / Source / reqtools / general.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-02  |  14.8 KB  |  628 lines

  1.  
  2. /* INCLUDES */
  3.  
  4. #include <exec/types.h>
  5. #include <exec/memory.h>
  6. #include <exec/execbase.h>
  7. #include <libraries/dos.h>
  8. #include <libraries/dosextens.h>
  9. #include <graphics/gfxmacros.h>
  10. #include <graphics/videocontrol.h>
  11. #include <graphics/view.h>
  12. #include <graphics/gfxbase.h>
  13. #include <libraries/gadtools.h>
  14. #include <intuition/intuition.h>
  15. #include <intuition/intuitionbase.h>
  16. #include <intuition/imageclass.h>
  17. #include <proto/graphics.h>
  18. #include <proto/exec.h>
  19. #include <proto/intuition.h>
  20. #include <proto/dos.h>
  21. #include <proto/gadtools.h>
  22. #include <proto/utility.h>
  23. #include <clib/macros.h>
  24. #include <string.h>
  25.  
  26. #include <libraries/reqtools.h>
  27. #include <proto/reqtools.h>
  28.  
  29. #ifdef _AROS
  30. #include <aros/asmcall.h>
  31. #endif
  32.  
  33. #include "filereq.h"
  34.  
  35. /****************************************************************************************/
  36.  
  37. /* Use ColorMap.Count to find out number of palette entried on screen */
  38. //#define DO_CM_DEPTH
  39.  
  40. /****************************************************************************************/
  41.  
  42. UWORD CHIP waitpointer[] =
  43. {
  44.     0x0000,0x0000,
  45.     0x0400,0x07C0,0x0000,0x07C0,0x0100,0x0380,0x0000,0x07E0,
  46.     0x07C0,0x1FF8,0x1FF0,0x3FEC,0x3FF8,0x7FDE,0x3FF8,0x7FBE,
  47.     0x7FFC,0xFF7F,0x7EFC,0xFFFF,0x7FFC,0xFFFF,0x3FF8,0x7FFE,
  48.     0x3FF8,0x7FFE,0x1FF0,0x3FFC,0x07C0,0x1FF8,0x0000,0x07E0,
  49.     0x0000,0x0000,
  50. };
  51.  
  52. /****************************************************************************************/
  53.  
  54. extern struct Library         *GadToolsBase;
  55. extern struct DosLibrary     *DOSBase;
  56. extern struct IntuitionBase     *IntuitionBase;
  57. extern struct GfxBase         *GfxBase;
  58. extern struct ReqToolsBase     *ReqToolsBase;
  59. #if defined(_AROS) || defined(__GNUC__)
  60. extern struct UtilityBase     *UtilityBase;
  61. #else
  62. extern struct Library         *UtilityBase;
  63. #endif
  64.  
  65. /****************************************************************************************/
  66.  
  67. int ASM SAVEDS GetVScreenSize (
  68.     REGPARAM(a0, struct Screen *, scr),
  69.     REGPARAM(a1, int *, width),
  70.     REGPARAM(a2, int *, height))
  71. {
  72.     struct ViewPortExtra     *vpe;
  73.     struct Rectangle         dispclip, *clip;
  74.     ULONG             getvpetags[3];
  75.     int             ht;
  76.  
  77.     getvpetags[0] = VTAG_VIEWPORTEXTRA_GET;
  78.     getvpetags[1] = (ULONG)&vpe;
  79.     getvpetags[2] = TAG_END;
  80.     
  81.     Forbid();
  82. #ifndef _AROS
  83. #warning No VideoControl in AROS, yet
  84.     if (IntuitionBase->FirstScreen == scr &&
  85.     VideoControl (scr->ViewPort.ColorMap, (struct TagItem *)getvpetags) == 0)
  86.     {
  87.     clip = &((struct ViewPortExtra *)getvpetags[1])->DisplayClip;
  88.     }
  89.     else
  90.     {
  91. #endif
  92.     QueryOverscan (GetVPModeID (&scr->ViewPort), &dispclip, OSCAN_TEXT);
  93.     clip = &dispclip;
  94. #ifndef _AROS
  95.     }
  96. #endif
  97.     Permit();
  98.     
  99.     *width = clip->MaxX - clip->MinX + 1;
  100.     *height = ht = clip->MaxY - clip->MinY + 1;
  101.     
  102.     if (scr->Width < *width) *width = scr->Width;
  103.     if (scr->Height < *height) *height = scr->Height;
  104.     
  105.     return ((ht >= 400) ? 4 : 2);
  106. }
  107.  
  108. /****************************************************************************************/
  109.  
  110. #undef ThisProcess()
  111. #define ThisProcess()        ( ( APTR ) FindTask( NULL ) )
  112.  
  113. /****************************************************************************************/
  114.  
  115. struct Screen *REGARGS GetReqScreen (
  116.     struct NewWindow *nw, struct Window **prwin, struct Screen *scr, char *pubname)
  117. {
  118.     struct Window  *win = *prwin;
  119.     struct Process *proc;
  120.  
  121.     if (!pubname && !scr && !win)
  122.     {
  123.     proc = ThisProcess();
  124.     if (proc->pr_Task.tc_Node.ln_Type == NT_PROCESS) win = proc->pr_WindowPtr;
  125.     }
  126.     
  127.     nw->Type = CUSTOMSCREEN;
  128.     if (!scr)
  129.     {
  130.     if (win && (ULONG)win != ~0) scr = win->WScreen;
  131.     else
  132.     {
  133.         if (!(scr = LockPubScreen (pubname)))
  134.         if (!(scr = LockPubScreen (NULL))) return (NULL);
  135.         
  136.         nw->Type = PUBLICSCREEN;
  137.         win = NULL;
  138.     }
  139.     }
  140.     *prwin = win;
  141.     return (nw->Screen = scr);
  142. }
  143.  
  144. /****************************************************************************************/
  145.  
  146. #ifndef DO_CM_DEPTH
  147. static int VpDepth (struct ViewPort *vp)
  148. {
  149.     ULONG     modeid = GetVPModeID (vp);
  150.     int     depth;
  151.  
  152.     depth = vp->RasInfo->BitMap->Depth;
  153.     
  154.     if (modeid & HAM_KEY) depth -= 2;
  155.     if (modeid & EXTRAHALFBRITE_KEY) depth = 5;
  156.  
  157.     if( depth > 8 )
  158.     {
  159.     depth = 8;
  160.     }
  161.  
  162.     return (depth);
  163. }
  164. #endif
  165.  
  166. /****************************************************************************************/
  167.  
  168. int REGARGS
  169. GetVpCM( struct ViewPort *vp, APTR *cmap)
  170. {
  171.  
  172. #ifdef DO_CM_DEPTH
  173.     int numcols;
  174.  
  175.     if( !vp->ColorMap )
  176.     {
  177.         return( 0 );
  178.     }
  179.  
  180.     numcols = vp->ColorMap->Count;
  181. #else
  182.     int depth, numcols;
  183.  
  184.     if( !vp->ColorMap )
  185.     {
  186.     return( 0 );
  187.     }
  188.  
  189.     depth = VpDepth (vp);
  190.     numcols = (1 << depth);
  191. #endif
  192.     if( GfxBase->LibNode.lib_Version >= 39 )
  193.     {
  194.     if( ( *cmap = AllocVec( ( numcols * 3 + 2 ) * 4, MEMF_PUBLIC | MEMF_CLEAR ) ) )
  195.     {
  196.         ( ( UWORD * ) ( *cmap ) )[ 0 ] = numcols;
  197.     }
  198.     }
  199.     else
  200.     {
  201.     *cmap = AllocVec( numcols * 2, MEMF_PUBLIC | MEMF_CLEAR );
  202.     }
  203.  
  204.     if( *cmap )
  205.     {
  206.     RefreshVpCM( vp, *cmap );
  207.     return( depth );
  208.     }
  209.     else
  210.     {
  211.     return( 0 );
  212.     }
  213. }
  214.  
  215. /****************************************************************************************/
  216.  
  217. void REGARGS
  218. RefreshVpCM( struct ViewPort *vp, APTR cmap )
  219. {
  220. #ifdef DO_CM_DEPTH
  221.     int numcols, i;
  222.  
  223.     numcols = vp->ColorMap->Count;
  224. #else
  225.     int i, depth, numcols;
  226.  
  227.     depth = VpDepth( vp );
  228.     numcols = ( 1 << depth );
  229. #endif
  230.     if( GfxBase->LibNode.lib_Version >= 39 )
  231.     {
  232.     GetRGB32( vp->ColorMap, 0, numcols, ( ( ULONG * ) cmap ) + 1 );
  233.     }
  234.     else
  235.     {
  236.     for( i = 0; i < numcols; i++ )
  237.     {
  238.         ( ( UWORD * ) cmap )[ i ] = GetRGB4( vp->ColorMap, i );
  239.     }
  240.     }
  241. }
  242.  
  243. /****************************************************************************************/
  244.  
  245. void REGARGS LoadCMap (struct ViewPort *vp, APTR cmap)
  246. {
  247.     if (GfxBase->LibNode.lib_Version >= 39) LoadRGB32 (vp, cmap);
  248. #ifdef DO_CM_DEPTH
  249.     else LoadRGB4 (vp, cmap, vp->ColorMap->Count);
  250. #else
  251.     else LoadRGB4 (vp, cmap, (1 << VpDepth (vp)));
  252. #endif
  253. }
  254.  
  255. /****************************************************************************************/
  256.  
  257. void REGARGS FreeVpCM (struct ViewPort *vp, APTR cmap, BOOL restore)
  258. {
  259.     if (restore && cmap) LoadCMap (vp, cmap);
  260.     FreeVec (cmap);
  261. }
  262.  
  263. /****************************************************************************************/
  264.  
  265. void REGARGS InitNewGadget (struct NewGadget *ng,
  266.         int x, int y, int w, int h, char *s, UWORD id)
  267. {
  268.     ng->ng_LeftEdge = x; ng->ng_TopEdge = y; ng->ng_Width = w; ng->ng_Height = h;
  269.     ng->ng_GadgetText = s; ng->ng_GadgetID = id;
  270. }
  271.  
  272. /****************************************************************************************/
  273.  
  274. struct TextFont * REGARGS GetReqFont (struct TextAttr *attr,
  275.         struct TextFont *deffont, int *fontheight, int *fontwidth, int allowprop)
  276. {
  277.     struct TextFont     *ft;
  278.     int         forcedef;
  279.  
  280.     forcedef = (rtLockPrefs()->Flags & RTPRF_DEFAULTFONT);
  281.     rtUnlockPrefs();
  282.  
  283.     ft = OpenFont (attr);
  284.     if (!ft || forcedef || (!allowprop && (ft->tf_Flags & FPF_PROPORTIONAL)))
  285.     {
  286.     if (ft) CloseFont (ft);
  287.     if (deffont) ft = deffont;
  288.     else ft = GfxBase->DefaultFont;
  289.     
  290.     attr->ta_Name = ft->tf_Message.mn_Node.ln_Name;
  291.     attr->ta_YSize = ft->tf_YSize;
  292.     attr->ta_Style = ft->tf_Style;
  293.     attr->ta_Flags = ft->tf_Flags;
  294.     ft = OpenFont (attr);
  295.     }
  296.     
  297.     if (ft)
  298.     {
  299.     *fontheight = ft->tf_YSize;
  300.     *fontwidth = ft->tf_XSize;
  301.     }
  302.     
  303.     return (ft);
  304. }
  305.  
  306. /****************************************************************************************/
  307.  
  308. struct IntuiMessage *REGARGS GetWin_GT_Msg (struct Window *win,
  309.                         struct Hook *hook, APTR req)
  310. {
  311.     struct IntuiMessage *imsg, *reqmsg;
  312.  
  313.     while ((imsg = (struct IntuiMessage *)GetMsg (win->UserPort)))
  314.     if ((reqmsg = ProcessWin_Msg (win, imsg, hook, req))) return (reqmsg);
  315.     
  316.     return (NULL);
  317. }
  318.  
  319. /****************************************************************************************/
  320.  
  321. struct IntuiMessage *REGARGS ProcessWin_Msg (struct Window *win,
  322.                         struct IntuiMessage *imsg, struct Hook *hook, APTR req)
  323. {
  324.     struct IntuiMessage *reqmsg;
  325.  
  326.     if (imsg->IDCMPWindow == win)
  327.     {
  328.     reqmsg = GT_FilterIMsg (imsg);
  329.     if (reqmsg) return (reqmsg);
  330.     ReplyMsg ((struct Message *)imsg);
  331.     }
  332.     else
  333.     {
  334.     if (hook) CallHookPkt (hook, req, imsg);
  335.     ReplyMsg ((struct Message *)imsg);
  336.     }
  337.     
  338.     return (NULL);
  339. }
  340.  
  341. /****************************************************************************************/
  342.  
  343. void REGARGS Reply_GT_Msg (struct IntuiMessage *reqmsg)
  344. {
  345.     ReplyMsg ((struct Message *)GT_PostFilterIMsg (reqmsg));
  346. }
  347.  
  348. /****************************************************************************************/
  349.  
  350. void REGARGS DoScreenToFront (struct Screen *scr, int nopop, int popup)
  351. {
  352.     ULONG noscrtofront;
  353.  
  354.     if (nopop) return;
  355.     
  356.     noscrtofront = (rtLockPrefs()->Flags & RTPRF_NOSCRTOFRONT);
  357.     rtUnlockPrefs();
  358.     
  359.     if (noscrtofront) return;
  360.     if (popup)
  361.         ScreenToFront (scr);
  362.     else
  363.         rtScreenToFrontSafely (scr);
  364. }
  365.  
  366.  
  367. /****************************************************************************************/
  368.  
  369. void REGARGS DoCloseWindow (struct Window *win, int idcmpshared)
  370. {
  371.     if (idcmpshared) rtCloseWindowSafely (win);
  372.     else CloseWindow (win);
  373. }
  374.  
  375. /****************************************************************************************/
  376.  
  377. struct BackFillMsg
  378. {
  379.     struct Layer *layer;
  380.     struct Rectangle bounds;
  381.     LONG offsetx;
  382.     LONG offsety;
  383. };
  384.  
  385. /****************************************************************************************/
  386.  
  387. #ifdef _AROS
  388. AROS_UFH3(void, WinBackFill,
  389.     AROS_UFHA(struct Hook *, hook, A0),
  390.     AROS_UFHA(struct RastPort *, the_rp, A2),
  391.     AROS_UFHA(struct BackFillMsg, *msg, A1))
  392. #else
  393. void SAVEDS ASM WinBackFill (
  394.     REGPARAM(a0, struct Hook *, hook),
  395.     REGPARAM(a2, struct RastPort *, the_rp),
  396.     REGPARAM(a1, struct BackFillMsg *, msg))
  397. #endif
  398. {
  399.     struct RastPort rp;
  400.  
  401.     memcpy( &rp, the_rp, sizeof( rp ) );
  402.     rp.Layer = NULL;
  403.     SetAPen (&rp, ((UWORD *)hook->h_Data)[BACKGROUNDPEN]);
  404.     mySetWriteMask (&rp, ~0);
  405.     RectFill (&rp, msg->bounds.MinX, msg->bounds.MinY,
  406.            msg->bounds.MaxX, msg->bounds.MaxY);
  407. }
  408.  
  409. /****************************************************************************************/
  410.  
  411. #ifdef _AROS
  412.  
  413. AROS_UFH3(void, PatternWinBackFill,
  414.     AROS_UFHA(struct Hook *, hook, A0),
  415.     AROS_UFHA(struct RastPort *, the_rp, A2),
  416.     AROS_UFHA(struct BackFillMsg, *msg, A1))
  417. {
  418.     struct RastPort rp;
  419.     UWORD pattern[] = {0xAAAA,0x5555};
  420.     
  421.     memcpy( &rp, the_rp, sizeof( rp ) );
  422.     rp.Layer = NULL;
  423.  
  424.     SetAPen (&rp, ((UWORD *)hook->h_Data)[BACKGROUNDPEN]);
  425.     SetBPen (&rp, ((UWORD *)hook->h_Data)[SHINEPEN]);
  426.     SetDrMd (&rp, JAM2);
  427.     
  428.     mySetWriteMask (&rp, ~0);
  429.     
  430.     SetAfPt(&rp, pattern, 1);
  431.     
  432.     RectFill (&rp, msg->bounds.MinX, msg->bounds.MinY,
  433.            msg->bounds.MaxX, msg->bounds.MaxY);
  434.            
  435.     SetAfPt(&rp, NULL, 0);
  436. }
  437.  
  438. #endif
  439.  
  440. /****************************************************************************************/
  441.  
  442. void REGARGS mySetWriteMask (struct RastPort *rp, ULONG mask)
  443. {
  444.     if (GfxBase->LibNode.lib_Version >= 39) SetWriteMask (rp, mask);
  445.     else SetWrMsk (rp, (UBYTE)mask);
  446. }
  447.  
  448. /****************************************************************************************/
  449.  
  450. struct Window *REGARGS OpenWindowBF (struct NewWindow *nw,
  451.             struct Hook *hook, UWORD *pens, ULONG *maskptr, WORD *zoom,
  452.             BOOL backfillpattern)
  453. {
  454.     struct RastPort *rp;
  455.     struct Window *win;
  456.     ULONG tags[5], mask;
  457.     UWORD maxpen = 0;
  458.     int i;
  459.  
  460. #ifdef _AROS
  461.     if (backfillpattern)
  462.     {
  463.         hook->h_Entry = (ULONG (*)())PatternWinBackFill;
  464.     } else
  465. #endif
  466.     hook->h_Entry = (ULONG (*)())WinBackFill;
  467.  
  468.     hook->h_Data = (void *)pens;
  469.  
  470.     tags[0] = WA_BackFill;
  471.     tags[1] = (ULONG)hook;
  472.     tags[2] = WA_Zoom;
  473.     tags[3] = (ULONG)zoom;
  474.     tags[4] = TAG_END;
  475.  
  476.     if( zoom )
  477.     {
  478.     if (IntuitionBase->LibNode.lib_Version >= 39)
  479.         zoom[0] = zoom[1] = ~0;
  480.     else
  481.     {
  482.         zoom[0] = nw->LeftEdge;
  483.         zoom[1] = nw->TopEdge;
  484.     }
  485.     }
  486.     else
  487.     {
  488.     tags[2] = TAG_IGNORE;
  489.     }
  490.  
  491.     if ((win = OpenWindowTagList (nw, (struct TagItem *)tags)))
  492.     {
  493.     rp = win->RPort;
  494.     for (i = 0; i <= HIGHLIGHTTEXTPEN; i++)
  495.         if (pens[i] > maxpen) maxpen = pens[i];
  496.         
  497.     mask = 1;
  498.     while (mask < maxpen)
  499.     {
  500.         mask <<= 1;
  501.         mask |= 1;
  502.     }
  503.     
  504.     mySetWriteMask (rp, mask);
  505.     if (maskptr) *maskptr = mask;
  506.     }
  507.     
  508.     return (win);
  509.  
  510. }
  511.  
  512. /****************************************************************************************/
  513.  
  514. int CheckReqPos (int reqpos, int reqdefnum, struct NewWindow *newwin)
  515. {
  516.     struct ReqDefaults *reqdefs;
  517.  
  518.     if (reqpos == REQPOS_DEFAULT)
  519.     {
  520.     reqdefs = &rtLockPrefs()->ReqDefaults[reqdefnum];
  521.     reqpos = reqdefs->ReqPos;
  522.     
  523.     if (reqpos <= REQPOS_CENTERSCR)
  524.     {
  525.         newwin->LeftEdge = 0;
  526.         newwin->TopEdge = 0;
  527.     }
  528.     else
  529.     {
  530.         newwin->LeftEdge = reqdefs->LeftOffset;
  531.         newwin->TopEdge = reqdefs->TopOffset;
  532.     }
  533.     
  534.     rtUnlockPrefs();
  535.     }
  536.     
  537.     return (reqpos);
  538. }
  539.  
  540. /****************************************************************************************/
  541.  
  542. /*********
  543. * Layout *
  544. *********/
  545.  
  546. /****************************************************************************************/
  547.  
  548. int REGARGS StrWidth_noloc (struct IntuiText *itxt, UBYTE *str)
  549. {
  550.     char labstr[100], *l;
  551.  
  552.     if (!str) return (0);
  553.     
  554.     /* Copy string, remove underscore */
  555.     l = labstr;
  556.     while (*str && *str != '_') *l++ = *str++;
  557.     
  558.     if (*str) while ((*l++ = *++str)); else *l = 0;
  559.     
  560.     itxt->IText = labstr;
  561.     
  562.     return (IntuiTextLength (itxt));
  563. }
  564.  
  565. /****************************************************************************************/
  566.  
  567. static int ObjectWidth (struct NewGadget *ng, int normalw, int normalh)
  568. {
  569.     if ((GadToolsBase->lib_Version >= 39) && (ng->ng_TextAttr->ta_YSize > normalh))
  570.     return (normalw + (ng->ng_TextAttr->ta_YSize - normalh) * 2);
  571.     
  572.     return (normalw);
  573. }
  574.  
  575. /****************************************************************************************/
  576.  
  577. static int ObjectHeight (struct NewGadget *ng, int normalh)
  578. {
  579.     if ((GadToolsBase->lib_Version >= 39) && (ng->ng_TextAttr->ta_YSize > normalh))
  580.     return (ng->ng_TextAttr->ta_YSize);
  581.     
  582.     return (normalh);
  583. }
  584.  
  585. /****************************************************************************************/
  586.  
  587. int CheckBoxWidth (struct NewGadget *ng)
  588. {
  589.     return (ObjectWidth (ng, CHECKBOX_WIDTH, CHECKBOX_HEIGHT));
  590. }
  591.  
  592. /****************************************************************************************/
  593.  
  594. int CheckBoxHeight (struct NewGadget *ng)
  595. {
  596.     return (ObjectHeight (ng, CHECKBOX_HEIGHT));
  597. }
  598.  
  599. /****************************************************************************************/
  600.  
  601. /*
  602.  * SIZEGAD HEIGHT
  603.  */
  604. LONG BottomBorderHeight (struct Screen *scr)
  605. {
  606.     struct DrawInfo     *dri;
  607.     APTR         obj;
  608.     LONG         h = 10;
  609.  
  610.     if ((dri = GetScreenDrawInfo (scr)))
  611.     {
  612.     if((obj = NewObject (NULL, "sysiclass", SYSIA_DrawInfo, dri,
  613.                         // Must be SYSISIZE_MEDRES!
  614.                         SYSIA_Size, SYSISIZE_MEDRES,
  615.                         SYSIA_Which, SIZEIMAGE,
  616.                         TAG_DONE)))
  617.     {
  618.         if (!GetAttr (IA_Height, obj, (ULONG *)&h))
  619.             h = 10;  // Probably not needed.. Or?
  620.         DisposeObject( obj );
  621.     }
  622.     FreeScreenDrawInfo (scr, dri);
  623.     }
  624.     return (h);
  625. }
  626.  
  627. /****************************************************************************************/
  628.